﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL = AccessDAL;
using CommonFunc;

namespace BLL
{
    public class NewsClass
    {
        DAL.NewsClassTableAdapters.NewsClassTableAdapter DNC =
            new DAL.NewsClassTableAdapters.NewsClassTableAdapter();
        DAL.NewsTableAdapters.NewsTableAdapter DN =
            new DAL.NewsTableAdapters.NewsTableAdapter();

        MODEL.NewsClass _MNC = new MODEL.NewsClass();


        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MNC"></param>
        /// <returns></returns>
        public MODEL.NewsClass GetDataByID(MODEL.NewsClass MNC)
        {
            try
            {
                DataTable DT = DNC.GetDataByID(MNC.NC_ID);

                _MNC.NC_ClassCode = DT.Rows[0]["NC_ClassCode"].ToString();
                _MNC.NC_ID = Convert.ToInt32(DT.Rows[0]["NC_ID"]);
                _MNC.NC_IsLock = Convert.ToBoolean(DT.Rows[0]["NC_IsLock"]);
                _MNC.NC_Level = Convert.ToInt32(DT.Rows[0]["NC_Level"]);
                _MNC.NC_ParentID = Convert.ToInt32(DT.Rows[0]["NC_ParentID"]);
                _MNC.NC_Title = DT.Rows[0]["NC_Title"].ToString();

                return _MNC;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加类别
        /// <summary>
        /// 添加信息
        /// </summary>
        /// <param name="MNC"></param>
        public void Add(MODEL.NewsClass MNC)
        {
            try
            {
                string classcode;
                int level;

                DataTable DT_GetDataByTitle_ParentID = DNC.GetDataByTitle_ParentID(MNC.NC_Title, MNC.NC_ID);

                if (DT_GetDataByTitle_ParentID.Rows.Count != 0)
                {
                    throw new Exception("此分类下，此类别名称已被占用");
                }

                DataTable DT_GetDataByParentID = DNC.GetDataByParentID(MNC.NC_ID);

                if (DT_GetDataByParentID.Rows.Count == 0)
                {
                    _MNC = GetDataByID(MNC);
                    classcode = ClassCode.CreateChildClassCode(_MNC.NC_ClassCode);
                    level = _MNC.NC_Level + 1;
                }
                else
                {
                    string OldClassCode = DT_GetDataByParentID.Rows[0]["NC_ClassCode"].ToString();
                    level = Convert.ToInt32(DT_GetDataByParentID.Rows[0]["NC_Level"]);
                    classcode = ClassCode.CreateNextClassCode(OldClassCode, level.ToString());
                }

                DNC.Add(MNC.NC_Title, MNC.NC_ID, classcode, level, MNC.NC_IsLock);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改类别
        /// <summary>
        /// 修改类别
        /// </summary>
        /// <param name="MNC"></param>
        public void Mod(MODEL.NewsClass MNC)
        {
            try
            {
                DNC.Mod(MNC.NC_Title, MNC.NC_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除类别
        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="MNC"></param>
        public void Del(MODEL.NewsClass MNC)
        {
            try
            {
                DataTable DT = DNC.GetDataByParentID(MNC.NC_ID);

                if (DT.Rows.Count != 0)
                {
                    throw new Exception("此分类下有子分类，不可删除，请清空子分类后重试");
                }

                DataTable DT2 = DN.GetDataByClassID(MNC.NC_ID);

                if (DT2.Rows.Count != 0)
                {
                    throw new Exception("此分类下有文章或回收站内有属于此类别文章，不可删除，请清空分类下文章后重试");
                }

                DataTable DT3 = DNC.GetDataByParentID(0);
                _MNC = GetDataByID(MNC);

                if (_MNC.NC_Level == 1 && DT3.Rows.Count == 1)
                {
                    throw new Exception("根分类不得为零项");
                }

                DNC.Del(MNC.NC_ID);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

    }
}
